<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends CI_Controller {

 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}
		$this->load->library('form_validation');		
		$this->load->model('client/client_model');		
		$this->load->model('ion_auth_model');
		$this->load->library('encrypt');
		$this->load->model('user/task_model');
		$this->load->model('user/user_model');
	}	
	
   function index()
   {
		 	$meta['page_title'] = "welcome Client |"; 
			$data['my_projects'] = $this->client_model->my_projects();
			
			$data['todos'] = $this->user_model->get_today_todo();
			
			$data['client_questions'] = $this->client_model->client_questions(10);
			
			$data['fav_projects'] = $this->user_model->get_fav_projects();
			$data['fav_tasks'] = $this->user_model->get_fav_tasks();
			
			$data['recent_tasks'] = $this->client_model->recent_tasks();			
			$data['recent_projects'] = $this->client_model->recent_projects();
			
		 	$this->load->view('common/header', $meta);
		  	$this->load->view('client/dashboard',$data);
		  	$this->load->view('common/footer');
			
   }
   
   function my_projects(){
   	$meta['page_title'] = 'My Projects';
   	$data['my_projects'] = $this->client_model->my_projects();
	$this->load->view('common/header', $meta);
		 $this->load->view('client/my-projects',$data);
		  $this->load->view('common/footer');
   }
   
   function add_client(){
   	if(!$this->ion_auth->is_admin()){
	redirect('client/client','refresh');
	}
	else {	
		
		$this->form_validation->set_rules('company_name', 'Company Name', 'required');
		$this->form_validation->set_rules('contact_name', 'Contact Name', 'required');
		$this->form_validation->set_rules('contact_number', 'Contact Number', 'required|numeric');
		$this->form_validation->set_rules('email_id', 'email_id', 'required|valid_email|is_unique[users.email]');
		$this->form_validation->set_rules('alt_email', 'Alternate email', 'required|valid_email');
		$this->form_validation->set_rules('password', 'password', 'required|min_length[8]');
		if($this->form_validation->run() == TRUE){
			
			$username = $this->input->post('contact_name');
			$password = $this->input->post('password');
			$email = $this->input->post('email_id');
			$group = 7; 
			
			/*$insert_user = array(
			'contact_name' => $this->input->post('contact_name')
			);*/
			
			$additional_data = array(
				'username' => $this->input->post('contact_name'),				
				'first_name' => $this->input->post('contact_name'),
				'last_name' => $this->input->post('contact_name'),
				'phone'      => $this->input->post('contact_number'),
				'active'	=> $this->input->post('status'),
				'company'	=> $this->input->post('company_name'),
				'email'		=> $this->input->post('email_id'),
				'created_on' => time(),
		    	'last_login' => time(),
				'ip_address' =>  $this->input->ip_address(),
				'password' => $this->ion_auth_model->hash_password($this->input->post('password')),
			);
			
			$client_id = $this->client_model->add_client($additional_data);
			
			$this->client_model->add_client_to_group($client_id);
			
			$insert_details = array(
			'client_id'=> $client_id,
			'access'=> $this->input->post('access'),
			'address'=> $this->input->post('address'),
			'city'=> $this->input->post('city'),
			'state'=> $this->input->post('state'),
			'country'=> $this->input->post('country'),
			'private_notes'=> nl2br($this->input->post('private_notes')),
				
			);
			$client_id = $this->client_model->client_details($insert_details);
			redirect('client/client/add_client','refresh');
			//exit;
		}
		
		$meta['page_title'] = "Add Client"; 
		$data['my_projects'] = $this->client_model->my_projects();
		$data['clients'] = $this->client_model->get_clients();
		$this->load->view('common/header', $meta);
		$this->load->view('client/add',$data);
		$this->load->view('common/footer');
		}
   }
   
   function edit($id = null){
   	$meta['page_title'] = "Edit Client";
	$edit['values'] = $this->client_model->get_client_details($id);
	
	$this->form_validation->set_rules('company_name', 'Company Name', 'required');
		$this->form_validation->set_rules('contact_name', 'Contact Name', 'required');
		$this->form_validation->set_rules('contact_number', 'Contact Number', 'required|numeric');
		$this->form_validation->set_rules('email_id', 'email_id', 'required|valid_email');		
		
	if($this->form_validation->run() == TRUE){
		
		$pwd  = $this->ion_auth_model->hash_password($this->input->post('password'));
		
		if(!empty($pwd)){		
		$user_update = array(
			'username' => $this->input->post('contact_name'),				
			'first_name' => $this->input->post('contact_name'),
			'last_name' => $this->input->post('contact_name'),
			'phone'      => $this->input->post('contact_number'),
			'active'	=> $this->input->post('status'),
			'company'	=> $this->input->post('company_name'),
			'email'		=> $this->input->post('email_id'),
			'password' => $pwd,
			);			
		}else{
			$user_update = array(
			'username' => $this->input->post('contact_name'),				
			'first_name' => $this->input->post('contact_name'),
			'last_name' => $this->input->post('contact_name'),
			'phone'      => $this->input->post('contact_number'),
			'active'	=> $this->input->post('status'),
			'company'	=> $this->input->post('company_name'),
			'email'		=> $this->input->post('email_id'),				
			);	
		}		
			
		$client_update = array(			
			'access'=> $this->input->post('access'),
			'address'=> $this->input->post('address'),
			'city'=> $this->input->post('city'),
			'state'=> $this->input->post('state'),
			'country'=> $this->input->post('country'),
			'private_notes'=> nl2br($this->input->post('private_notes')),
		);
		
		$client_ID  = $this->input->post('client_id');
		$this->client_model->update($user_update,$client_update,$client_ID);
		redirect("client/client/edit/$id");
		
	}	
   		$this->load->view('common/header', $meta);
		$this->load->view('client/edit',$edit);
		$this->load->view('common/footer');
   }
   
}
	
	?>